package com.exrobot.module.rbt.service.memberaddress;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.exrobot.module.rbt.enums.RbtAddressTypeEnum;
import com.exrobot.module.rbt.framework.util.CommonUtils;
import com.exrobot.module.rbt.service.mqtt.MqttMessageService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.exrobot.module.rbt.controller.admin.memberaddress.vo.*;
import com.exrobot.module.rbt.dal.dataobject.memberaddress.MemberAddressDO;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.util.object.BeanUtils;

import com.exrobot.module.rbt.dal.mysql.memberaddress.MemberAddressMapper;

import static com.exrobot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.exrobot.framework.common.util.collection.CollectionUtils.convertList;
import static com.exrobot.module.rbt.enums.ErrorCodeConstants.*;

/**
 * 客户地址信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MemberAddressServiceImpl implements MemberAddressService {

    @Resource
    private MemberAddressMapper memberAddressMapper;

    @Resource
    private MqttMessageService mqttMessageService;

    @Override
    public Long createMemberAddress(MemberAddressSaveReqVO createReqVO) {
        // 插入
        MemberAddressDO memberAddress = BeanUtils.toBean(createReqVO, MemberAddressDO.class);
        memberAddressMapper.insert(memberAddress);
        RbtAddressTypeEnum addressTypeEnum = RbtAddressTypeEnum.validateVal(createReqVO.getAddressType());
        // 返回
        return memberAddress.getId();
    }

    @Override
    public void updateMemberAddress(MemberAddressSaveReqVO updateReqVO) {
        // 校验存在
        validateMemberAddressExists(updateReqVO.getId());
        // 更新
        MemberAddressDO updateObj = BeanUtils.toBean(updateReqVO, MemberAddressDO.class);
        memberAddressMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberAddress(Long id) {
        // 校验存在
        validateMemberAddressExists(id);
        // 删除
        memberAddressMapper.deleteById(id);
    }

    @Override
    public void deleteMemberAddressListByIds(List<Long> ids) {
        // 删除
        memberAddressMapper.deleteByIds(ids);
    }


    private void validateMemberAddressExists(Long id) {
        if (memberAddressMapper.selectById(id) == null) {
            throw exception(RBT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public MemberAddressDO getMemberAddress(Long id) {
        return memberAddressMapper.selectById(id);
    }

    @Override
    public MemberAddressDO getMemberAddressByPoint(String point) {
        String[] address = CommonUtils.getAddressByPoint(point);
        if(address == null) {
            return null;
        }
        LambdaQueryWrapper<MemberAddressDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberAddressDO::getAreaNo,address[0]);
        queryWrapper.eq(MemberAddressDO::getFloorNo,address[1]);
        queryWrapper.eq(MemberAddressDO::getRoomNo,address[2]);
        return memberAddressMapper.selectOne(queryWrapper);
    }

    @Override
    public PageResult<MemberAddressDO> getMemberAddressPage(MemberAddressPageReqVO pageReqVO) {
        return memberAddressMapper.selectPage(pageReqVO);
    }

}